<?php
	function load_villages() {
		global $mobdb;
		
		// Load Villages
		$lines = gzfile('http://s11.tribalwars.es/map/village.txt.gz');
		if(!is_array($lines))
			die("Archivo no se ha podido abrir");
		
		$villages = 0; 
		foreach($lines as $line) {
			list($id, $name,$x, $y, $tribe, $points, $bonus) = explode(',', $line);
			
			$name = urldecode($name);
			$name = addslashes($name);
			
			$data = array();
			$data['id']     = $id;
			$data['name']   = $name;
			$data['x']      = $x;
			$data['y']      = $y;
			$data['tribe']  = $tribe;
			$data['points'] = $points;
			$data['bonus']  = $bonus;
			
			$mobdb->insert('village', $data);
			
			$villages ++;
		}
		
		echo "Se han almacenado $villages pueblos<br/>";
	}
	
	function load_tribe() {
		global $mobdb;
		
		// Load Villages
		$lines = gzfile('http://s11.tribalwars.es/map/tribe.txt.gz');
		if(!is_array($lines))
			die("Archivo no se ha podido abrir");
		
		$tribes = 0; 
		foreach($lines as $line) {
			list($id, $name, $ally, $villages, $points, $rank) = explode(',', $line);
			
			$name = urldecode($name);
			$name = addslashes($name);
			
			$data = array();
			$data['id']       = $id;
			$data['name']     = $name;
			$data['ally']     = $ally;
			$data['villages'] = $villages;
			$data['points']   = $points;
			$data['rank']     = $rank;
			
			$mobdb->insert('tribe', $data);
			
			$tribes ++;
		}
		
		echo "Se han almacenado $tribes jugadores<br/>";
	}
	
	function load_ally() {
		global $mobdb;
		
		// Load Villages
		$lines = gzfile('http://s11.tribalwars.es/map/ally.txt.gz');
		if(!is_array($lines))
			die("Archivo no se ha podido abrir");
		
		$ally = 0; 
		foreach($lines as $line) {
			list($id, $name, $tag, $members, $villages, $points, $all_points, $rank) = explode(',', $line);
			
			$name = urldecode($name);
			$name = addslashes($name);
			
			$data = array();
			$data['id']         = $id;
			$data['name']       = $name;
			$data['tag']        = $tag;
			$data['members']    = $members;
			$data['villages']   = $villages;
			$data['points']     = $points;
			$data['all_points'] = $all_points;
			$data['rank']       = $rank;
			
			$mobdb->insert('ally', $data);
			
			$ally ++;
		}
		
		echo "Se han almacenado $ally tribus<br/>";
	}
	
	function get_ally($id) {
		global $mobdb;
		
		$rows = $mobdb->query("SELECT * FROM ally WHERE id=$id");
		if ($rows > 0)
			return $mobdb->last_result[0];
		else
			return null;
	}
	
	function get_ranges() {
		global $mobdb;
		
		$rows = $mobdb->query("SELECT MIN(x) AS minx, MAX(x) as maxx, MIN(y) as miny, MAX(y) as maxy FROM village");
		
		if ($rows > 0) {
			$ranges = array();
			$ranges['minx'] = $mobdb->last_result[0]->minx;
			$ranges['maxx'] = $mobdb->last_result[0]->maxx;
			$ranges['miny'] = $mobdb->last_result[0]->miny;
			$ranges['maxy'] = $mobdb->last_result[0]->maxy;
			
			return $ranges;
		} else
			return null;
	}
	
	function put_map($width) {
		global $mobdb;
		//Get ranges
		$ranges = get_ranges();
        //echo "<pre>";
		//var_dump($ranges);
        //echo "</pre>";
		//Get Allys (session)
		$selected = $_SESSION['selected'];
		
		//Load Allys
		$allys = array();
		foreach($selected as $sel) {
			//Load Villages of each tribe
            $sql = "SELECT t.name, v.name as villagename, v.x, v.y FROM tribe AS t, village AS v WHERE v.tribe = t.id AND t.ally = $sel->id";
			$rows = $mobdb->query($sql);
            //echo "$sql<hr>";
            
			if ($rows > 0) {
				$pos = count($allys);
				
				$allys[$pos]->color    = $sel->color;
				$allys[$pos]->villages = $mobdb->last_result;
			}
		}
		
		//Calcule coords
		$dot_size_x = $width / (float)($ranges['maxx'] - $ranges['minx']);
		$dot_size_y = $width / (float)($ranges['maxy'] - $ranges['miny']);
		
		//Paint Villages
		foreach($allys as $ally) {
			
            $color = $ally->color;
            
			foreach($ally->villages as $village) {
                $x = abs($ranges['minx']) + $village->x;
                $y = abs($ranges['miny']) + -$village->y;
				//Text
				$name = $village->name . " - " . $village->villagename . "";
                //echo "$name {$village->x},{$village->y} is now $x , $y<br/>";
                echo "$x|*|$y|*|$name|*|$color|||";
			}
		}
		

	}
	
	function get_color($num) {
		switch($num) {
			case 0:
				return '#0000FF';
			case 1:
				return '#F40000';
			case 2:
				return '#E932D6';
			default:
				return '#000000';
		}
	}
?>
